package st.magnuzon.book.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import st.magnuzon.book.client.managed.request.ApplicationRequestFactory;
import st.magnuzon.book.client.managed.request.AuthorProxy;
import st.magnuzon.book.client.managed.request.AuthorRequest;
import st.magnuzon.book.client.managed.ui.AuthorDetailsView;
import st.magnuzon.book.client.managed.ui.AuthorEditView;
import st.magnuzon.book.client.managed.ui.AuthorListView;
import st.magnuzon.book.client.managed.ui.AuthorMobileDetailsView;
import st.magnuzon.book.client.managed.ui.AuthorMobileEditView;
import st.magnuzon.book.client.scaffold.ScaffoldApp;
import st.magnuzon.book.client.scaffold.place.CreateAndEditProxy;
import st.magnuzon.book.client.scaffold.place.FindAndEditProxy;
import st.magnuzon.book.client.scaffold.place.ProxyPlace;

public class AuthorActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public AuthorActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new AuthorDetailsActivity((EntityProxyId<AuthorProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? AuthorMobileDetailsView.instance() : AuthorDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<st.magnuzon.book.client.managed.request.AuthorProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<AuthorProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        AuthorEditView.instance().setCreating(true);
        final AuthorRequest request = requests.authorRequest();
        Activity activity = new CreateAndEditProxy<AuthorProxy>(AuthorProxy.class, request, ScaffoldApp.isMobile() ? AuthorMobileEditView.instance() : AuthorEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(AuthorProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new AuthorEditActivityWrapper(requests, ScaffoldApp.isMobile() ? AuthorMobileEditView.instance() : AuthorEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        AuthorEditView.instance().setCreating(false);
        EntityProxyId<AuthorProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<AuthorProxy>(proxyId, requests, ScaffoldApp.isMobile() ? AuthorMobileEditView.instance() : AuthorEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(AuthorProxy proxy) {
                AuthorRequest request = requests.authorRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new AuthorEditActivityWrapper(requests, ScaffoldApp.isMobile() ? AuthorMobileEditView.instance() : AuthorEditView.instance(), activity, proxyId);
    }
}
